package com.springboot.utils;

import com.springboot.config.security.admin.AdminSecurityConfig;
import com.springboot.config.security.admin.AdministratorDetails;
import com.springboot.config.security.wx.WxUserDetails;
import com.springboot.entity.Administrator;
import com.springboot.entity.WxUser;
import com.springboot.repository.AdministratorRepository;
import com.springboot.repository.WxUserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @descrition:
 * @author: lzh
 */
@Component
public class UserUtil {


    static WxUserRepository wxUserRepository;

    static AdministratorRepository administratorRepository;

    @Autowired
    WxUserRepository wRepository;

    @Autowired
    AdministratorRepository aRepository;

    @PostConstruct
    public void initWxUser() {
        wxUserRepository = wRepository;
    }

    @PostConstruct
    public void initAdmin() {
        administratorRepository = aRepository;
    }

    public static WxUser getCurrentUser() {
        WxUserDetails wxUserDetails = (WxUserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return wxUserRepository.findByOpenid(wxUserDetails.getOpenid());
    }

    public static Administrator getCurrentAdmin() {
        AdministratorDetails administratorDetails =
                (AdministratorDetails)SecurityContextHolder.getContext()
                        .getAuthentication().getPrincipal();
        return administratorRepository.findByUsername(administratorDetails.getUsername());
    }
}
